package likou.tanxin;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: Tangxz
 * @email: 1171702529@qq.com
 * @cate: 2020/11/14 22:09
 */
public class _936 {
    public int[] movesToStamp(String stamp, String target) {
        //将给定的印章和目标转换为char数组
        char[] s = stamp.toCharArray();
        char[] t = target.toCharArray();
        boolean[] visited = new boolean[t.length];
        //list的res用以存放逆向的印章顺序
        List<Integer> res = new ArrayList();
        //stars是存放总共用星星替代了几个
        int stars = 0;
        //没有全部覆盖完之前，不退出
        while (stars < t.length) {
            //flag本次是否有覆盖发生
            boolean donereplace = false;
            //System.out.println("donereplace");
            //target从头到尾进行一次遍历，寻找符合全部符合stamp的字样
            for (int i = 0; i <= t.length - s.length; i++) { //小于等于
                //若存在可以替代的字符段
                //System.out.println("for");
                if (visited[i] == false && canReplace(s, t, i)) {
                    //执行替代方法
                    stars = doreplace(s, t, i, stars);
                    donereplace = true;
                    res.add(i);
                    visited[i] = true;
                }
                if (stars == t.length) {
                    int[] resv = new int[res.size()];
                    for (int j = 0; j < res.size(); j++) {//res的
                        resv[j] = res.get(res.size() - j - 1);
                    }
                    return resv;
                }
            }
            //System.out.println("if");
            if (!donereplace) {
                return new int[0];
            }
            //return new int[2];
        }
        return new int[3];
    }

    private boolean canReplace(char[] s, char[] t, int i) {
        for (int j = 0; j < s.length; j++) {
            if (t[j + i] != '*' && t[j + i] != s[j]) {
                //System.out.println("false");
                return false;
            }
        }
        return true;
    }

    private int doreplace(char[] s, char[] t, int i, int count) {
        for (int k = 0; k < s.length; k++) {
            if (t[i + k] != '*') {
                t[i + k] = '*';
                count++;
            }
        }
        return count;
    }
}
